package com.cyrus.vueboot.controller.excel;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.cyrus.vueboot.pojo.auth.Admin;
import com.cyrus.vueboot.pojo.dpt.DepartmentUser;
import com.cyrus.vueboot.pojo.log.OpsLog;
import com.cyrus.vueboot.pojo.log.loginLog;
import com.cyrus.vueboot.service.AdminService;
import com.cyrus.vueboot.service.DepartmentUserService;
import com.cyrus.vueboot.service.LoginLogService;
import com.cyrus.vueboot.service.OpsLogService;
import com.cyrus.vueboot.service.impl.OpsLogServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: Cyrus
 * @CreateDate: 2020/11/16
 */
@Controller
public class OpsController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private LoginLogService loginLogService;

    @Autowired
    private OpsLogService opsLogService;

    @Autowired
    private DepartmentUserService departmentUserService;
    // 通过工具类创建writer
    ExcelWriter writer = ExcelUtil.getWriter(true);

    /**
     * 导出Excel表格
     */
    @RequestMapping("/outPutExcel")
    @ResponseBody
    public void outPutExcel(HttpServletResponse response) throws Exception {

            List<Admin> list = adminService.list();

            List<Admin> rows = CollUtil.newArrayList(list);


            //自定义标题别名
            writer.addHeaderAlias("id", "id");
            writer.addHeaderAlias("username", "用户名");
            writer.addHeaderAlias("password", "密码");
            writer.addHeaderAlias("phone", "手机号");
            writer.addHeaderAlias("email", "邮箱");
            writer.addHeaderAlias("status", "当前状态");
            writer.addHeaderAlias("authority", "权限名");
            writer.addHeaderAlias("isShow", "是否显示");


            // 合并单元格后的标题行，使用默认标题样式
            writer.merge(7, "用户数据表");
            // 一次性写出内容，使用默认样式，强制输出标题
            writer.write(rows, true);

            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=userData.xlsx");
            ServletOutputStream out = response.getOutputStream();
            writer.flush(out, true);
            writer.close();
            IoUtil.close(out);
    }

    @RequestMapping("/outPutLoginLogExcel")
    @ResponseBody
    public void outPutLoginLogExcel(HttpServletResponse response) throws Exception {

        List<loginLog> list = loginLogService.list();

        List<loginLog> rows = CollUtil.newArrayList(list);


        //自定义标题别名
        writer.addHeaderAlias("id", "id");
        writer.addHeaderAlias("logindate", "登陆日期");
        writer.addHeaderAlias("loginuser", "登陆用户");
        writer.addHeaderAlias("serialid", "流水号");

        // 合并单元格后的标题行，使用默认标题样式
        writer.merge(3, "登陆日志表");
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=LoginLogData.xlsx");
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }

    @RequestMapping("/outPutOpsLogExcel")
    @ResponseBody
    public void outPutOpsLogExcel(HttpServletResponse response) throws Exception {
        List<OpsLog> list = opsLogService.list();
        List<OpsLog> rows = CollUtil.newArrayList(list);
        //自定义标题别名
        writer.addHeaderAlias("id", "id");
        writer.addHeaderAlias("ip", "登录ip");
        writer.addHeaderAlias("module", "模块");
        writer.addHeaderAlias("description", "描述");
        writer.addHeaderAlias("method", "方法");
        writer.addHeaderAlias("params", "参数");
        writer.addHeaderAlias("username", "操作用户");
        writer.addHeaderAlias("opstime", "操作时间");

        // 合并单元格后的标题行，使用默认标题样式
        writer.merge(7, "操作日志表");
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=OpsLogData.xlsx");
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }

    @RequestMapping("/outPutDepartmentUserExcel")
    @ResponseBody
    public void outPutDepartmentUserExcel(HttpServletResponse response) throws Exception {
        List<DepartmentUser> list = departmentUserService.list();
        List<DepartmentUser> rows = CollUtil.newArrayList(list);
        //自定义标题别名
        writer.addHeaderAlias("id", "id");
        writer.addHeaderAlias("username", "员工姓名");
        writer.addHeaderAlias("dptName", "员工部门");
        writer.addHeaderAlias("phone", "员工手机");
        writer.addHeaderAlias("address", "员工地址");
        writer.addHeaderAlias("isDelete", "是否在职");

        // 合并单元格后的标题行，使用默认标题样式
        writer.merge(5, "员工信息表");
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=DepartmentUserData.xlsx");
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }
}


